package com.qf.web.school_college.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.utils.ResultUtil;
import com.qf.utils.ResultVo;
import com.qf.web.school_college.entity.ListParams;
import com.qf.web.school_college.entity.SchoolCollege;
import com.qf.web.school_college.service.SchoolCollegeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author xufuhan
 * @Date 2024/9/12 11:08
 */

@RestController
@RequestMapping("/api/college")
public class SchoolCollegeController {

    @Autowired
    private SchoolCollegeService schoolCollegeService;

    //新增
    @PostMapping
    public ResultVo add(@RequestBody SchoolCollege schoolCollege){
        boolean save = schoolCollegeService.save(schoolCollege);
        if (save){
            return ResultUtil.success("新增学院成功");
        }else {
            return ResultUtil.error("新增学院失败");
        }
    }

    //编辑
    @PutMapping
    public ResultVo edit(@RequestBody SchoolCollege schoolCollege){
        boolean save = schoolCollegeService.updateById(schoolCollege);
        if (save){
            return ResultUtil.success("编辑学院成功");
        }else {
            return ResultUtil.error("编辑学院失败");
        }
    }

    //删除
    @DeleteMapping("/{collegeId}")
    public ResultVo delete(@PathVariable("collegeId") Long collegeId){
        boolean remove = schoolCollegeService.removeById(collegeId);
        if (remove){
            return ResultUtil.success("删除学院成功");
        }else {
            return ResultUtil.error("删除学院失败");
        }
    }

    //获取列表
    @GetMapping("/list")
    public ResultVo getList(ListParams params){
        QueryWrapper<SchoolCollege> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(params.getCollegeName())){
            wrapper.lambda().like(SchoolCollege::getCollegeName,params.getCollegeName());
        }
        //分页
        IPage<SchoolCollege> page=new Page<>(params.getCurrentPage(),params.getPageSize());

        IPage<SchoolCollege> list = schoolCollegeService.page(page, wrapper);
        return ResultUtil.success("查询成功",list);
    }
}
